<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/1
 * Time: 17:13
 */
namespace App\Utils;


use App\Exceptions\RequestException;
use Illuminate\Http\UploadedFile;

class Upload
{

    /**
     * @param $file
     * @return string
     * @throws RequestException
     */
    public static function upload($file)
    {

        if ($file instanceof UploadedFile) {
            // 获取文件相关信息
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
        } else {
            // 兼容网络资源
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            $realPath = $file;
        }

        $disk = \Storage::disk('oss');
        // 上传文件
        $filename =  '/' . date('Ymd') . '/' . \Str::Random(32) . '.' . $ext;
        $bool = $disk->put($filename, file_get_contents($realPath), ['header' => ['Content-Type:image/jpg']]);
        if($bool){
            return 'http://'.env('OSS_URL') . $filename;
        }

//        if (\Storage::put($filename, file_get_contents($realPath))) {
//            return self::BASE_PATH . $filename;
//        }

        throw new RequestException(trans('message.file.error'));
    }


}